/* The BasicMeleeAttack class is a simple, instant melee attack to the closest target.
 * It extends the Ability superclass for basic ability behaviour 
 *
 * Author : Reinier
 */

class BasicMeleeAttack extends Ability{

public var attackEffect : GameObject;	/* The basic effect for using the ability */
public var damageEffect : GameObject;	/* The effect on the target */

/* The update function needs to call the superclass's update since the update message is not send to any children */
function Update() {
	super.Update();
}

/* The use function provides this ability's functionality and returns a boolean whether is has actually been used or not
 * for a basic melee attack the closest viable target is picked an is dealt an instant amount of damage.
 * An pre-determined effect is also displayed.
 */ 
function Use() : boolean {

	/* The cooldown is checked, the function will return false if the cooldown is still pending and cannot be used */
	if(currentCooldown <= 0) {
		/* The target which we want to damage will be assigned later in the function */
		var target : GameObject;
		/* The distance is set to maximum so the first target's distance is used for comparison */
		var distance : float = Mathf.Infinity;
		var viableTargets : Collider[] = GetHits();
	
		/* The viable targets array is run through and the closest target is determined */
		for(i = 0; i < viableTargets.Length; i++) {
			var thisDistance : float = Vector3.Distance(viableTargets[i].transform.position, transform.position);
			if(thisDistance < distance) {
				distance = thisDistance;
				target = viableTargets[i].gameObject;
			} 
		}
	
		/* do damage as long as there is a target and it has health */
		if(target) {
			var targetStats : CharacterStats = target.GetComponent(CharacterStats);
			if(targetStats) {
				var damageAmount : int;
				/* The attack modifier method is called to influence the damage proportional to the amount of attack stat */
				damageAmount = ApplyAttackModifier();
				targetStats.OnDamage(damageAmount, gameObject);
			}
			
			/*displays a damage effect on the target if one is assigned */
			if(damageEffect) {
				var tempEffect : GameObject = Instantiate(damageEffect, target.transform.position, target.transform.rotation);
				/* TODO The translate effect should be dynamic, proportional to the user's height */
				tempEffect.transform.Translate(Vector3(0,3,0));
				tempEffect.transform.parent = target.transform;
			}
		}
	
		/* Display the slash effect if it is assigned */
		if(attackEffect) {		
			var tempParticles : GameObject = Instantiate(attackEffect, gameObject.transform.position, gameObject.transform.rotation);
			tempParticles.transform.parent = gameObject.transform;
			/* TODO The translate effect should be dynamic, proportional to the user's height */
			tempParticles.transform.Translate(Vector3(0,2.5,2));
			tempParticles.transform.parent = null;
		}
		else {
			Debug.Log("There is no graphical effect assigned for the ability 'Basic Melee attack'");
		}
		
		/* If the ability has been used, return true */
		return true;
	}
	else {
		/* return false if the ability is still on cooldown */
		return false;
	}
}

}